package com.bsb.biz.service;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.apache.shiro.SecurityUtils;
import org.springframework.stereotype.Service;

import com.bsb.base.common.config.Global;
import com.bsb.base.common.config.WithdrawStatus;
import com.bsb.base.common.util.StringUtils;
import com.bsb.base.mapper.BsbUserMapper;
import com.bsb.base.mapper.BsbWithdrawMapper;
import com.bsb.base.model.BsbUser;
import com.bsb.base.model.BsbWithdraw;
import com.bsb.base.model.BsbWithdrawExample;

import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.mp.bean.pay.result.WxEntPayResult;

@Service
public class BsbWithdrawService {

	private static final Logger log = Logger.getLogger(BsbWithdrawService.class);

	@Resource
	private BsbWithdrawMapper bsbWithdrawMapper;
	@Resource
	private BsbUserMapper bsbUserMapper;
	@Resource
	private PaymentService paymentService;

	public void addBsbWithdraw(BsbWithdraw bsbWithdraw) {
		bsbWithdrawMapper.insertSelective(bsbWithdraw);
	}

	public void delBsbWithdrawByUuid(String uuid) {
		bsbWithdrawMapper.deleteByPrimaryKey(uuid);
	}

	public void updateBsbWithdraw(BsbWithdraw bsbWithdraw) {
		bsbWithdrawMapper.updateByPrimaryKeySelective(bsbWithdraw);
	}

	public void updateBsbWithdrawByExample(BsbWithdraw bsbWithdraw, BsbWithdrawExample bsbWithdrawExample) {
		bsbWithdrawMapper.updateByExampleSelective(bsbWithdraw, bsbWithdrawExample);
	}

	public BsbWithdraw getBsbWithdrawByUuid(String uuid) {
		return bsbWithdrawMapper.selectByPrimaryKey(uuid);
	}

	public List<BsbWithdraw> getPageBsbWithdraw(BsbWithdrawExample bsbWithdrawExample) {
		return bsbWithdrawMapper.selectPage(bsbWithdrawExample);
	}

	public int countBsbWithdraw(BsbWithdrawExample bsbWithdrawExample) {
		return bsbWithdrawMapper.countByExample(bsbWithdrawExample);
	}

	public List<BsbWithdraw> selectByExample(BsbWithdrawExample example) {
		return bsbWithdrawMapper.selectByExample(example);
	}

	public void updateNopass(String uuid, String reason) {
		BsbWithdraw selectByPrimaryKey = bsbWithdrawMapper.selectByPrimaryKey(uuid);
		if (null == selectByPrimaryKey) {
			return;
		}
		BsbWithdraw with = new BsbWithdraw();
		with.setUuid(uuid);
		with.setStatus(WithdrawStatus.NOPASS.getIndex());
		with.setReason(reason);
		with.setUpdateid((String) SecurityUtils.getSubject().getPrincipal());

		String userid = selectByPrimaryKey.getUserid();
		BsbUser user = bsbUserMapper.selectByPrimaryKey(userid);
		if (null == user) {
			return;
		}
		Double balance = user.getBalance();
		Double money = selectByPrimaryKey.getMoney();
		BsbUser u = new BsbUser();
		u.setUuid(userid);
		u.setBalance(balance + money);
		u.setFreezePrice(0.00);

		bsbUserMapper.updateByPrimaryKeySelective(u);
		bsbWithdrawMapper.updateByPrimaryKeySelective(with);
		// TODO 发送短信通知
	}

	public void updatePass(String uuid) throws Exception{
		BsbWithdraw selectByPrimaryKey = bsbWithdrawMapper.selectByPrimaryKey(uuid);
		if (null == selectByPrimaryKey) {
			return;
		}
		String userid = selectByPrimaryKey.getUserid();
		BsbUser user = bsbUserMapper.selectByPrimaryKey(userid);
		if (null == user) {
			return;
		}
		String partner_trade_no = selectByPrimaryKey.getUuid();
		String openid = selectByPrimaryKey.getOpenid();
		Double money = selectByPrimaryKey.getMoney();
		String ip = selectByPrimaryKey.getIp();
		WxEntPayResult entPay = null;
		try {
			entPay = paymentService.entPay(partner_trade_no, openid, money, ip);
			log.warn("提现审核微信返回报文信息："+entPay.toString());
			String resultCode = entPay.getResultCode();
			String returnCode = entPay.getReturnCode();
			if (Global.SUCCESS.equals(returnCode)) {
				if (Global.SUCCESS.equals(resultCode)) {
					BsbWithdraw withdraw = new BsbWithdraw();
					withdraw.setUuid(uuid);
					withdraw.setWxid(entPay.getPaymentNo());
					withdraw.setSucDate(new Date());
					withdraw.setStatus(WithdrawStatus.SUCESS.getIndex());
					withdraw.setUpdateid((String) SecurityUtils.getSubject().getPrincipal());
					bsbWithdrawMapper.updateByPrimaryKeySelective(withdraw);
					
					BsbUser u = new BsbUser();
					u.setUuid(userid);
					u.setFreezePrice(0.0);
					bsbUserMapper.updateByPrimaryKeySelective(u);
					return;
				}
			}else{
				String returnMsg = entPay.getReturnMsg();
				String errCode = entPay.getErrCode();
				String errCodeDes = entPay.getErrCodeDes();
				if (StringUtils.isNotEmpty(errCode)) {
					log.error("returnMsg:" + returnMsg + ",errCode:" + errCode + ",errCodeDes" + errCodeDes);
					BsbWithdraw withdraw = new BsbWithdraw();
					withdraw.setUuid(uuid);
					withdraw.setStatus(WithdrawStatus.EXCEPTION.getIndex());
					withdraw.setErrorCode(errCode);
					withdraw.setRemarks(errCodeDes);
					withdraw.setUpdateid((String) SecurityUtils.getSubject().getPrincipal());
					bsbWithdrawMapper.updateByPrimaryKeySelective(withdraw);
				}
				return;
			}
		} catch (WxErrorException e) {
			e.printStackTrace();
		} 
	}
}
